package com.cskaoyan.service.promotion;

import com.cskaoyan.bean.goods.Goods;
import com.cskaoyan.bean.goods.GoodsExample;
import com.cskaoyan.bean.promotion.*;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.GrouponMapper;
import com.cskaoyan.mapper.GrouponRulesMapper;
import com.cskaoyan.utils.DataUtils;
import com.cskaoyan.vo.promotion.GrouponListRecordVo;
import com.cskaoyan.vo.promotion.GrouponListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * @description:
 * @autyor:caicaicai
 */
@Service
public class GrouponServcieImpl implements GrouponService {

    @Autowired
    GrouponRulesMapper grouponRulesMapper;

    @Autowired
    GrouponMapper grouponMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public GrouponListVo list(Integer page, Integer limit, String sort, String order, Integer goodsId) {
        PageHelper.startPage(page, limit);
        //构造条件
        GrouponRulesExample grouponRulesExample = new GrouponRulesExample();
        grouponRulesExample.setOrderByClause(sort + " " + order);
        //分页 排序 条件查询（根据goodsId精确查询） 非逻辑删除deleted=false
        //where deleted=false and title like "%title%" and subtitle like "%subTitle%"
        GrouponRulesExample.Criteria criteria = grouponRulesExample.createCriteria();//sql语句的拼接${}
        criteria.andDeletedEqualTo(false);//逻辑删除为false
        if(goodsId != null && !"".equals(goodsId)){
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<GrouponRules> grouponRules = grouponRulesMapper.selectByExample(grouponRulesExample);

        //获取total
        PageInfo<GrouponRules> grouponRulesPageInfo = new PageInfo<>(grouponRules);
        long total = grouponRulesPageInfo.getTotal();

        //封装数据到响应javaben
        GrouponListVo grouponListVo = new GrouponListVo();
        grouponListVo.setItems(grouponRules);
        grouponListVo.setTotal(total);
        return grouponListVo;
    }

    @Override
    public int update(GrouponRules grouponRules) {
        //先查goodsId即goods表中是否存在id
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria criteria = goodsExample.createCriteria();
        criteria.andIdEqualTo(grouponRules.getGoodsId());
        long total = goodsMapper.countByExample(goodsExample);

        if(total == 1){//id/goodsId存在goods表中，则更新
            int affectedRows = grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
            if(affectedRows == 1){
                return 200;
            }
            return 404;
        }
        return 10000;//goodsId不存在，参数值不对
    }

    @Override
    public GrouponRules create(GrouponRules grouponRules) {
        //通过goodsId查goods表的name、picUrl,封装给goodsName、picUrl
        Goods goods = goodsMapper.selectByPrimaryKey(grouponRules.getGoodsId());

        //goodsId是否存在？不存在,参数不存在
        if(goods == null){
            return null;
        }
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setPicUrl(goods.getPicUrl());

        Date nowDate = DataUtils.getCurrentTime();
        grouponRules.setAddTime(nowDate);
        grouponRules.setUpdateTime(nowDate);

        //插入数据后，主键被塞回输入映射的对象中
        grouponRulesMapper.insertSelective(grouponRules);
        return grouponRules;
    }

    @Override
    public int delete(GrouponRules grouponRules) {
        //逻辑删除，即修改属性deleted=true
        grouponRules.setDeleted(true);
        int affectedRows =grouponRulesMapper.updateByPrimaryKeySelective(grouponRules);
        if(affectedRows == 1){
            return 200;
        }
        return 404;
    }

    @Override
    public GrouponListRecordVo listRecord(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        //构造条件
        GrouponExample grouponExample = new GrouponExample();
        grouponExample.setOrderByClause(sort + " " + order);
        //分页 排序 查询 非逻辑删除deleted=false
        //where deleted=false
        GrouponExample.Criteria criteria = grouponExample.createCriteria();//sql语句的拼接${}
        criteria.andDeletedEqualTo(false);//逻辑删除为false
        List<Groupon> groupons = grouponMapper.selectByExample(grouponExample);

        //获取total
        PageInfo<Groupon> grouponPageInfo = new PageInfo<>(groupons);
        long total = grouponPageInfo.getTotal();

        GrouponListRecordVo grouponListRecordVo = new GrouponListRecordVo();
        grouponListRecordVo.setItems(groupons);
        grouponListRecordVo.setTotal(total);
        return grouponListRecordVo;
    }
}
